package com.huanghuai.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@Slf4j
public class CaptchaController {

    @GetMapping("/code/image")
    public void getCaptchaCode(HttpServletRequest servletRequest,HttpServletResponse servletResponse) throws IOException {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200,100,2,2);
        String code = circleCaptcha.getCode();
        log.info("生成的图片验证码：{}",code);
        //将验证码存储在session中
        servletRequest.getSession().setAttribute("CAPTCHA",code);
        //将图片输出到响应流里，参数一，图片，二。图片格式，三，响应流
        ImageIO.write(circleCaptcha.getImage(),"png",servletResponse.getOutputStream());
    }

}
